package com.itheima.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.DTO.SetmealDTO;
import com.itheima.common.entity.PageResult;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SetmealServiceImp implements SetmealService {

    @Autowired
    private SetmealMapper SetmealMapper;

    //分页查询
    public PageResult pageQuery(SetmealDTO setmealDTO) {
        Integer currentPage = setmealDTO.getCurrentPage();
        Integer pageSize = setmealDTO.getPageSize();
        String queryString = setmealDTO.getQueryString();

        PageHelper.startPage(currentPage, pageSize);
        Page<Setmeal> page = SetmealMapper.pageQuery(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //根据id查询
    public Setmeal findById(Integer id) {
        return SetmealMapper.findById(id);
    }


    @Transactional
    public void edit(Setmeal setmeal, String checkGroupIds) {
        //修改套餐表
        SetmealMapper.edit(setmeal);
        //删除套餐和检查组的关联关系
        SetmealMapper.deleteCheckGroupAndSetmeal(setmeal.getId());
        //建立套餐和检查组的关联关系
        if (checkGroupIds != null && checkGroupIds.length() > 0) {
            String[] ids = checkGroupIds.split(",");
            for (String id : ids){
                SetmealMapper.setSetmealAndCheckGroup(setmeal.getId(),Integer.parseInt(id));
            }
        }
    }

    /**
     * 新增套餐
     * @param setmeal
     * @param checkGroupIds
     */
    @Transactional
    public void add(Setmeal setmeal, String checkGroupIds) {
        //添加套餐
        SetmealMapper.add(setmeal);
        //建立套餐和检查组的关联关系
        if (checkGroupIds != null && checkGroupIds.length() > 0) {
            String[] ids = checkGroupIds.split(",");
            for (String id : ids){
                SetmealMapper.setSetmealAndCheckGroup(setmeal.getId(),Integer.parseInt(id));
            }
        }
    }


    /**
     * 获取套餐预约数量数据
     */
    public Map<String, Object> getSetmealReport(){

        List<String> names = SetmealMapper.getSetmealNames();
        List<Map<String, Object>> values = SetmealMapper.values();
        Map<String, Object> data = new HashMap<>();
        data.put("setmealNames",names);
        data.put("setmealCount",values);
        return data;
    }




}
